package com.androidx.p013lv.base.glide.decrypt;

import android.util.Log;
import com.androidx.p013lv.base.utils.ImageCryptUtils;
import com.androidx.p013lv.base.utils.UiBuildConfig;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.HttpException;
import com.bumptech.glide.load.data.DataFetcher;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.util.ContentLengthInputStream;
import com.bumptech.glide.util.Preconditions;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

/* renamed from: com.androidx.lv.base.glide.decrypt.OkHttpStreamFetcher */
public class OkHttpStreamFetcher implements DataFetcher<InputStream>, Callback {
    private static final String TAG = "OkHttpFetcher";
    private volatile Call call;
    private DataFetcher.DataCallback<? super InputStream> callback;
    private final Call.Factory client;
    private ResponseBody responseBody;
    private InputStream stream;
    private final GlideUrl url;

    public OkHttpStreamFetcher(Call.Factory factory, GlideUrl glideUrl) {
        this.client = factory;
        this.url = glideUrl;
    }

    @Override // com.bumptech.glide.load.data.DataFetcher
    public void loadData(Priority priority, DataFetcher.DataCallback<? super InputStream> dataCallback) {
        Request.Builder url2 = new Request.Builder().url(this.url.toStringUrl());
        for (Map.Entry<String, String> entry : this.url.getHeaders().entrySet()) {
            url2.addHeader(entry.getKey(), entry.getValue());
        }
        Request build = url2.build();
        this.callback = dataCallback;
        this.call = this.client.newCall(build);
        this.call.enqueue(this);
    }

    @Override // okhttp3.Callback
    public void onFailure(Call call2, IOException iOException) {
        if (Log.isLoggable(TAG, 3)) {
            Log.d(TAG, "OkHttp failed to obtain result", iOException);
        }
        this.callback.onLoadFailed(iOException);
    }

    /* JADX WARNING: Removed duplicated region for block: B:40:0x009e  */
    /* JADX WARNING: Removed duplicated region for block: B:43:0x00a6  */
    /* JADX WARNING: Removed duplicated region for block: B:47:0x00b1 A[SYNTHETIC, Splitter:B:47:0x00b1] */
    /* JADX WARNING: Removed duplicated region for block: B:52:0x00b9 A[Catch:{ IOException -> 0x00b5 }] */
    /* JADX WARNING: Removed duplicated region for block: B:61:? A[RETURN, SYNTHETIC] */
    @Override // okhttp3.Callback
    public void onResponse(Call call2, Response response) {
        ByteArrayInputStream byteArrayInputStream;
        Throwable th;
        IOException e;
        this.responseBody = response.body();
        if (response.isSuccessful()) {
            InputStream obtain = ContentLengthInputStream.obtain(this.responseBody.byteStream(), ((ResponseBody) Preconditions.checkNotNull(this.responseBody)).contentLength());
            this.stream = obtain;
            int i = 100;
            byte[] bArr = new byte[100];
            ByteArrayOutputStream byteArrayOutputStream = null;
            try {
                int read = obtain.read(bArr);
                while (read < 100 && read != -1) {
                    int read2 = this.stream.read(bArr, read, 100 - read);
                    if (read2 == -1) {
                        this.callback.onLoadFailed(new IllegalStateException());
                        return;
                    }
                    read += read2;
                }
                byte[] xorEncode = ImageCryptUtils.xorEncode(bArr, 100, UiBuildConfig.ENCRYPT_IMAGE_KEY);
                ByteArrayOutputStream byteArrayOutputStream2 = new ByteArrayOutputStream();
                try {
                    byteArrayOutputStream2.write(xorEncode, 0, 100);
                    while (true) {
                        int read3 = this.stream.read(xorEncode);
                        if (read3 != -1) {
                            i += read3;
                            byteArrayOutputStream2.write(xorEncode, 0, read3);
                        } else {
                            ByteArrayInputStream byteArrayInputStream2 = new ByteArrayInputStream(byteArrayOutputStream2.toByteArray());
                            try {
                                this.callback.onDataReady(ContentLengthInputStream.obtain(byteArrayInputStream2, (long) i));
                                try {
                                    byteArrayOutputStream2.close();
                                    byteArrayInputStream2.close();
                                    return;
                                } catch (IOException e2) {
                                    e2.printStackTrace();
                                    return;
                                }
                            } catch (IOException e3) {
                                byteArrayOutputStream = byteArrayOutputStream2;
                                byteArrayInputStream = byteArrayInputStream2;
                                e = e3;
                                try {
                                    e.printStackTrace();
                                    if (byteArrayOutputStream != null) {
                                        byteArrayOutputStream.close();
                                    }
                                    if (byteArrayInputStream != null) {
                                        byteArrayInputStream.close();
                                        return;
                                    }
                                    return;
                                } catch (Throwable th2) {
                                    th = th2;
                                    if (byteArrayOutputStream != null) {
                                    }
                                    if (byteArrayInputStream != null) {
                                    }
                                    throw th;
                                }
                            } catch (Throwable th3) {
                                byteArrayOutputStream = byteArrayOutputStream2;
                                byteArrayInputStream = byteArrayInputStream2;
                                th = th3;
                                if (byteArrayOutputStream != null) {
                                    try {
                                        byteArrayOutputStream.close();
                                    } catch (IOException e4) {
                                        e4.printStackTrace();
                                        throw th;
                                    }
                                }
                                if (byteArrayInputStream != null) {
                                    byteArrayInputStream.close();
                                }
                                throw th;
                            }
                        }
                    }
                } catch (IOException e5) {
                    e = e5;
                    byteArrayInputStream = null;
                    byteArrayOutputStream = byteArrayOutputStream2;
                    e.printStackTrace();
                    if (byteArrayOutputStream != null) {
                    }
                    if (byteArrayInputStream != null) {
                    }
                } catch (Throwable th4) {
                    th = th4;
                    byteArrayInputStream = null;
                    byteArrayOutputStream = byteArrayOutputStream2;
                    if (byteArrayOutputStream != null) {
                    }
                    if (byteArrayInputStream != null) {
                    }
                    throw th;
                }
            } catch (IOException e6) {
                e = e6;
                byteArrayInputStream = null;
                e.printStackTrace();
                if (byteArrayOutputStream != null) {
                }
                if (byteArrayInputStream != null) {
                }
            } catch (Throwable th5) {
                th = th5;
                byteArrayInputStream = null;
                if (byteArrayOutputStream != null) {
                }
                if (byteArrayInputStream != null) {
                }
                throw th;
            }
        } else {
            this.callback.onLoadFailed(new HttpException(response.message(), response.code()));
        }
    }

    @Override // com.bumptech.glide.load.data.DataFetcher
    public void cleanup() {
        try {
            if (this.stream != null) {
                this.stream.close();
            }
        } catch (IOException unused) {
        }
        ResponseBody responseBody2 = this.responseBody;
        if (responseBody2 != null) {
            responseBody2.close();
        }
        this.callback = null;
    }

    @Override // com.bumptech.glide.load.data.DataFetcher
    public void cancel() {
        Call call2 = this.call;
        if (call2 != null) {
            call2.cancel();
        }
    }

    @Override // com.bumptech.glide.load.data.DataFetcher
    public Class<InputStream> getDataClass() {
        return InputStream.class;
    }

    @Override // com.bumptech.glide.load.data.DataFetcher
    public DataSource getDataSource() {
        return DataSource.LOCAL;
    }
}
